package com.wufun.overseas.main.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.wufun.baselibrary.download.CommonDownloadTask;
import com.wufun.baselibrary.impl.AdapterInterface;
import com.wufun.baselibrary.utils.LogUtils;
import com.wufun.overseas.R;
import com.wufun.overseas.adapters.PlayMainItemAdapter;
import com.wufun.overseas.common.TabTypeInterface;
import com.wufun.overseas.main.activity.PlayMainActivtiy;
import com.wufun.overseas.main.base.MyFragment;
import com.wufun.overseas.models.db.DownLoadModel;
import com.wufun.overseas.models.event.DownloadResultEvent;
import com.wufun.overseas.tools.DownloadManager;
import com.wufun.overseas.tools.MyDialog;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by tangbl on 2017/12/2 0002.
 */

public class PlayMainItemFragment extends MyFragment implements AdapterInterface
        , PlayMainItemAdapter.PlayAdapterInterface {

    @Bind(R.id.playmain_listview)
    ListView playmainListview;

    private int typeId;

    private PlayMainItemAdapter mAdapter;
    private List<DownLoadModel> playDataList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_playmainitem, null);
        ButterKnife.bind(this, view);
        Bundle b = getArguments();
        typeId = b.getInt("typeId");
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        getDBDownloadData();
    }

    private void getDBDownloadData() {
        if (mAdapter!=null) {
            playDataList.clear();
            List<DownLoadModel> tempList = DownloadManager.getInstance().getDownloadByType(typeId);
            Set set = new LinkedHashSet<DownLoadModel>();
            set.addAll(tempList);
            playDataList.addAll(set);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void initWidget(View... v) {
        super.initWidget(v);
        mAdapter = new PlayMainItemAdapter(getActivity(), playDataList, this, this);
        playmainListview.setAdapter(mAdapter);

        getDBDownloadData();
    }

    @Override
    public void fragmentEvent(DownloadResultEvent eventData) {
        super.fragmentEvent(eventData);
        switch (eventData.status) {
            case -99:
                getDBDownloadData();
                break;
            case CommonDownloadTask.STATUS_FINISH:
                getDBDownloadData();
                LogUtils.e("fragmentEvent>>>>>typeId:"+typeId);
                if (eventData.type == TabTypeInterface.TYPE_LOCAL) {
                    showToast(eventData.url.substring(eventData.url.lastIndexOf("/") + 1)
                            + getActivity().getResources().getString(R.string.toast_rom_su_str));
                } else {
                    showToast(eventData.url.substring(eventData.url.lastIndexOf("/") + 1)
                            + getActivity().getResources().getString(R.string.download_su_str));
                }
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onAdapterItemClick(View view, int position) {
        DownLoadModel start = mAdapter.getItem(position);
        switch (start.getStatus()) {
            case CommonDownloadTask.STATUS_FINISH:
                StartGame(start.getDownloadUrl(), start.getGamePath()+"/"+start.getGameZipName().replace(".zip",""));
                break;
        }
    }

    @Override
    public void onAdapterItemLongChildClick(View view, int position) {
        DownLoadModel start = mAdapter.getItem(position);
        wranDialog(R.string.dialog_single_delete_content, false, start.getDownloadUrl());
    }

    MyDialog mDialog = null;
    private void wranDialog(int resId, final boolean isAll, final String url) {
        mDialog = new MyDialog(getActivity(), R.style.MyDialog);
        mDialog.setContentView(R.layout.bind_dialog);

        Button dialog_button_cancel = (Button) mDialog.findViewById(R.id.dialog_button_cancel);
        Button dialog_button_ok = (Button) mDialog.findViewById(R.id.dialog_button_ok);
        TextView tip_title = (TextView) mDialog.findViewById(R.id.tip_title);
        TextView dialog_content = (TextView) mDialog.findViewById(R.id.dialog_content);
        TextView dialog_desci = (TextView) mDialog.findViewById(R.id.dialog_desci);

        tip_title.setText(getResources().getString(R.string.dialog_title));
        dialog_content.setText(getResources().getString(resId));
        dialog_desci.setVisibility(View.GONE);
        dialog_button_cancel.setText(getResources().getString(R.string.dialog_ok));
        dialog_button_ok.setText(getResources().getString(R.string.dialog_cancle));

        dialog_button_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogDismiss();
                if (isAll) {
                    DownloadManager.getInstance().deleteAllFinishDownload();
                } else {
                    DownloadManager.getInstance().deleteDownload(url);
                }
                new Handler().postAtTime(new Runnable() {
                    @Override
                    public void run() {
                        getDBDownloadData();
                        ((PlayMainActivtiy)getActivity()).onUserEvent(new DownloadResultEvent(-99, ""));
                    }
                }, 200);
            }
        });
        dialog_button_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogDismiss();
            }
        });
        if (null != mDialog) {
            if (mDialog.isShowing()) return;
            mDialog.show();
        }
    }

    private void dialogDismiss() {
        if (null != mDialog && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }
}
